#include "screenrectitem.h"

ScreenRectItem::ScreenRectItem(QGraphicsItem *parent, QGraphicsScene *scene)
    : QGraphicsRectItem(parent, scene)
{
    QPen newPen = pen();
    newPen.setColor(QColor(255,255,255));
    newPen.setWidthF(2/parent->scale());
    newPen.setJoinStyle(Qt::MiterJoin);
    setPen(newPen);
    setCursor(QCursor(Qt::OpenHandCursor));
}

void ScreenRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    setCursor(QCursor(Qt::ClosedHandCursor));
    lastMousePosition = event->pos();
}

void ScreenRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    setCursor(QCursor(Qt::OpenHandCursor));
}

void ScreenRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
        QPointF newPosition(pos() - (lastMousePosition-event->pos()));
        newPosition.setX(Utils::clampF(newPosition.x(),1,parentItem()->boundingRect().width()-rect().width()));
        newPosition.setY(Utils::clampF(newPosition.y(),1,parentItem()->boundingRect().height()-rect().height()));

        setPos(newPosition);

        lastMousePosition.setX(Utils::clampF(event->pos().x(),1,parentItem()->boundingRect().width()));
        lastMousePosition.setX(Utils::clampF(event->pos().y(),1,parentItem()->boundingRect().height()));

        emit screenMove(QPointF(pos().x()+(rect().width()/2),
                                pos().y() + ((rect().height()+150)/2)));

}

void ScreenRectItem::screenPanned(QPointF newPosition)
{
    setPos(newPosition.x()-(rect().width()/2)+1,newPosition.y()-(rect().height()/2)-74);
}
